package com.j2110.controller;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.lang.reflect.Method;


public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取访问路径的uri
        String path = req.getRequestURI();
        //截取路径获取方法名
        String methodName = path.substring(path.lastIndexOf("/") + 1);
        //获取调用service方法对象的字节码对象
        Class<? extends BaseServlet> clz = this.getClass();
        //获取要调用的方法
        try {
            Method method = clz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //调用方法
            method.invoke(this, req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
